package de.hs_mannheim.srn.prototyp.model;
import java.io.File;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class KeyPackage {
	private String filePath;
	private byte[] fileKey;
	
	public String getFilePath() {
		return this.filePath;
	}
	
	/**
	 * Initializes a new key package
	 * @param filePath to the encrypted file
	 * @param fileKey to decrypt the file
	 */
	public KeyPackage(String filePath, byte[] fileKey) {
		this.filePath = filePath;
		this.fileKey = fileKey;
	}
	
	
	/**
	 * Decrypt the package to the given path inside your home directory
	 * @param targetPath
	 * @return False if decryption fails, otherwise true
	 */
	public boolean decrypt(String targetPath) {
		File src = new File(this.filePath);
		File target = new File(targetPath);
		if(!src.isFile()) return false;
		if(!target.isFile()) return false;
		
		SecretKeySpec key = new SecretKeySpec(this.fileKey, "AES");
		Cipher cipher;
		
		byte[] result, readBuffer = null;
		
		try {
			cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");
			cipher.init(Cipher.DECRYPT_MODE, key);
			result = cipher.doFinal(readBuffer);
		} catch (Exception e) {
			throw new RuntimeException("Spurious serialisation error", e);
		}
		return true;
	}
	private byte[] decryptFileKey(Key key) {
		
	}
	@Override
	public boolean equals(Object obj) {
		KeyPackage keyPackage;
		if (obj instanceof KeyPackage){
			keyPackage = (KeyPackage) obj;
		} else { 
			return false;
		}

		if (!(this.getFilePath().equalsIgnoreCase(keyPackage.getFilePath()))){
			return false;
		}

		return true;
	}
	@Override
	public int hashCode() {
		int result = 17;
		result = 31 * result + filePath.hashCode();
		return result;
	}
	private byte[] decrypt
}
